﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Entities;
using System.Data.SqlClient;
using fideeraSoft.Utils;
using fideeraSoft.DAO;

namespace fideeraSoft.DAOimpl
{
    public class RemitoEntradaXLoteDAOSQLimpl : RemitoEntradaXLoteDAO
    {

        #region ALTA RemitoEntrada X Lote
        public void altaRemitoEntrada_X_Lote(RemitoEntrada remitoEntrada, Lote lote, int idRemitoEntradaXLote)
        {
            SqlConnection conn = null;
            try
            {
                conn = SQLclass.conectarSQL();
                conn.Open();

              //  CLogger.Append("[altaRemitoEntrada_X_Lote] ID : " + idRemitoEntradaXLote, CLogger.INFO);      //DEBUG

                //string query = "SET IDENTITY_INSERT RemitoEntrada_X_Lote ON; INSERT INTO RemitoEntrada_X_Lote (lote,remitoEntrada) VALUES (@IdLote,@IdRemitoEntrada)";
                string query = "SET IDENTITY_INSERT RemitoEntrada_X_Lote ON; INSERT INTO RemitoEntrada_X_Lote (ID,lote,remitoEntrada) VALUES (@IdMXL,@IdLote,@IdRemitoEntrada)";

                SqlCommand cmd = new SqlCommand(query, conn);

                cmd.Parameters.AddWithValue("@IdMXL", idRemitoEntradaXLote);
                cmd.Parameters.AddWithValue("@IdRemitoEntrada", remitoEntrada.IdRemitoEntrada);

                cmd.Parameters.AddWithValue("@IdLote", lote.IdLote);

                


                cmd.ExecuteNonQuery();
                //cmd.Parameters.Clear();
                // cmd.CommandText = "SELECT @@IDENTITY";
                // int insertID=Convert.ToInt32(cmd.ExecuteScalar());

                //  cmd.Dispose();
                cmd = null;

            }
            catch (Exception e)
            {
                CLogger.Append("[altaRemitoEntrada_X_Lote] Exception alta: " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[altaRemitoEntrada_X_Lote] Exception alta: " + e);
            }
            finally
            {
                conn.Close();
            }

        }

        #endregion


        #region BUSCAR ULTIMO ID DE RemitoEntrada X Lote
        public int buscarNuevoUltimoIdRemitoEntradaXLoteEnBD()
        {
            int ID = 0;
            SqlConnection conn = SQLclass.conectarSQL();
            try
            {
                conn.Open();

                string query = "SELECT TOP 1 ID FROM RemitoEntrada_X_Lote ORDER BY ID DESC ";
                SqlCommand cmd = new SqlCommand(query, conn);

                SQLclass.commandQuery(conn, cmd, query);
                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {

                    while (reader.Read())
                    {
                        ID = reader.GetInt32(0);
                        //  CLogger.Append("[buscarNuevoUltimoIdProveedorEnBD] ID : " + ID, CLogger.ERROR);      //DEBUG
                    }
                    ID++;
                }
                else
                {
                    ID = 1;
                }
            }
            catch (Exception e)
            {
                CLogger.Append("[buscarNuevoUltimoIdRemitoEntradaXLoteEnBD] Exception : " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[buscarNuevoUltimoIdRemitoEntradaXLoteEnBD] Exception: " + e);
            }
            finally
            {
                conn.Close();
            }
            // CLogger.Append("[buscarNuevoUltimoIdProveedorEnBD] RETURN ID : " + ID, CLogger.ERROR);      //DEBUG
            return ID;
        }

        #endregion

         #region BUSCAR Lista dE RemitoEntrada X Lote por ID
        public List<RemitoEntrada_X_Lotes> getListaRemitoEntradaXLotesPorID(string idRemito)
        {

            List<RemitoEntrada_X_Lotes> lstRemXLote;
            SqlConnection conn = SQLclass.conectarSQL();
            try
            {
                conn.Open();

                string query = "SELECT * FROM RemitoEntrada_X_Lote WHERE remitoEntrada='"+idRemito+"'";
                SqlCommand cmd = new SqlCommand(query, conn);

                SQLclass.commandQuery(conn, cmd, query);
                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {
                    lstRemXLote = new List<RemitoEntrada_X_Lotes>();
                    while (reader.Read())
                    {
                        RemitoEntrada_X_Lotes remXLotes = new RemitoEntrada_X_Lotes();

                        remXLotes.RemitoEntradaXLotes = reader.GetInt32(0);
                        remXLotes.Lote = reader.GetInt32(1);
                        remXLotes.RemitoEntrada = reader.GetInt32(2);

                        lstRemXLote.Add(remXLotes);
                        //  CLogger.Append("[buscarNuevoUltimoIdProveedorEnBD] ID : " + ID, CLogger.ERROR);      //DEBUG
                    }                    
                }
                else
                {
                    lstRemXLote=null;
                }
            }
            catch (Exception e)
            {
                CLogger.Append("[getListaRemitoEntradaXLotesPorID] Exception : " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[getListaRemitoEntradaXLotesPorID] Exception: " + e);
            }
            finally
            {
                conn.Close();
            }
            // CLogger.Append("[buscarNuevoUltimoIdProveedorEnBD] RETURN ID : " + ID, CLogger.ERROR);      //DEBUG
            return lstRemXLote;
        }

        #endregion


        #region ELIMINAR REMITO X LOTE POR ID
        public void eliminarRemitoEntradaXLotePorID(int id)
        {
            SqlConnection conn = SQLclass.conectarSQL();
            try
            {
                conn.Open();

                string query = "DELETE FROM RemitoEntrada_X_Lote WHERE ID='" + id.ToString() + "'";
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.ExecuteNonQuery();

            }
            catch (Exception e)
            {
                CLogger.Append("[eliminarRemitoEntradaXLotePorID] Exception : " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[eliminarRemitoEntradaXLotePorID] Exception: " + e);
            }
            finally
            {
                conn.Close();
            }
        }

        #endregion


        #region ELIMINAR TODOS LOS REMITO X LOTE EN DB
        public void eliminarTodosLosRemitosEntradaXLoteEnDb()
        {
            SqlConnection conn = SQLclass.conectarSQL();
            try
            {
                conn.Open();

                string query = "DELETE FROM RemitoEntrada_X_Lote";
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.ExecuteNonQuery();

            }
            catch (Exception e)
            {
                CLogger.Append("[eliminarTodosLosRemitosEntradaXLoteEnDb] Exception : " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[eliminarTodosLosRemitosEntradaXLoteEnDb] Exception: " + e);
            }
            finally
            {
                conn.Close();
            }
        }

        #endregion
    }
}
